package com.myandroid.custombookmark;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;

import android.database.Cursor;
import android.database.SQLException;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import android.net.Uri;
import android.text.TextUtils;

public class BookmarkProvider extends ContentProvider {
	//Database
	private static final String DATABASE_NAME		= "bookmarks.db";
	private static final int	DATABASE_VERSION	= 1;
	private SQLiteDatabase		mDB;
	private DatabaseHelper		mDBHelper;
	
	private class DatabaseHelper extends SQLiteOpenHelper	{
		public DatabaseHelper(Context context, String name, 
								CursorFactory factory, int version) {			
			super(context, name, factory, version);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(Bookmark.sites._CREATE);			
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + Bookmark.sites._TABLENAME);
			onCreate(db);
		}
	}
	
	@Override
	public boolean onCreate() {
		//Open Database
		mDBHelper 	= new DatabaseHelper(getContext(), DATABASE_NAME, null, DATABASE_VERSION);
		mDB			= mDBHelper.getWritableDatabase();
		return mDB !=null? true :false;			
	}
	
	private static final int SITES		= 0;
	private static final int SITE_ID	= 1;
	private static UriMatcher matcher;
	
	static {
		matcher	= new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI(Bookmark.AUTHORITY, "sites", SITES);
		matcher.addURI(Bookmark.AUTHORITY, "sites/#", SITE_ID);		
	}
	
	@Override
	public String getType(Uri uri) {
		switch(matcher.match(uri)) {
			case SITES :
				return "vnd.myandroid.cursor.dir/vnd.myandroid.bookmark.sites";
			case SITE_ID :
				return "vnd.myandroid.cursor.item/vnd.myandroid.bookmark.sites";
		}
		return null;
	}
	
	@Override
	public Uri insert(Uri uri, ContentValues values) {
		String tableName;
		Uri contentUri;
		long id;
		
		switch(matcher.match(uri)) {
			case SITES :
				tableName	= Bookmark.sites._TABLENAME;
				contentUri	= Bookmark.sites.CONTENT_URI;
				break;
			default:
				throw new IllegalArgumentException();
		}
		
		id	= mDB.insert(tableName, null, values);
		if(id > 0) {
			Uri itemUri = ContentUris.withAppendedId(contentUri, id);
			getContext().getContentResolver().notifyChange(itemUri, null);
			return itemUri;
		}else {
			throw new SQLException();
		}
	}
	
	@Override
	public int update(Uri uri, ContentValues values, String selection,
													String[] selectionArgs) {
		String 	tableName;
		String 	id;
		String 	whereSegment;
		int 	count;
		
		switch(matcher.match(uri)) {
			case SITES :
				tableName 		= Bookmark.sites._TABLENAME;
				whereSegment	= selection;
				break;
			case SITE_ID :
				tableName		= Bookmark.sites._TABLENAME;
				id				= uri.getPathSegments().get(1);
				whereSegment	= "_id = " + id +
							(!TextUtils.isEmpty(selection) ? "AND (" + selection + ')' : "");
				break;
			default :
				throw new IllegalArgumentException();
		}
		
		count	= mDB.update(tableName, values, whereSegment, selectionArgs);
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		String 	tableName;
		String 	id;
		String 	whereSegment;
		int		count;
		
		switch(matcher.match(uri)){
			case SITES :
				tableName 		= Bookmark.sites._TABLENAME;
				whereSegment	= selection;
				break;
			case SITE_ID :
				tableName		= Bookmark.sites._TABLENAME;
				id				= uri.getPathSegments().get(1);
				whereSegment	= "_id = " + id +
					(!TextUtils.isEmpty(selection) ? "AND (" + selection + ')' : "");
				break;
			default :
				throw new IllegalArgumentException();
		}
		
		count	= mDB.delete(tableName, whereSegment, selectionArgs);
		getContext().getContentResolver().notifyChange(uri, null);
		
		return count;		
	}
	
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
							String[] SelectionArgs, String sortOrder) {
		
		SQLiteQueryBuilder	qb	= new SQLiteQueryBuilder();
		switch(matcher.match(uri)) {
			case SITES :
				qb.setTables(Bookmark.sites._TABLENAME);
				break;
			case SITE_ID :
				qb.setTables(Bookmark.sites._TABLENAME);
				qb.appendWhere("_id ="+uri.getPathSegments().get(1));
				break;
			default :
				throw new IllegalArgumentException();			
		}
		
		Cursor c	= qb.query(mDB, projection, selection, SelectionArgs, null, null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);
		
		return c;
	}
}